package jtk.project4.fleet.task;

import java.util.List;

import jtk.project4.fleet.domain.PmHist;
import jtk.project4.fleet.event.CostStatisticEvent;
import jtk.project4.fleet.event.CostStatisticEvent.CostStatisticEventType;
import jtk.project4.fleet.service.DatabaseService;
import nl.coderight.jazz.Task;
import nl.coderight.jazz.event.MessageEvent;
import nl.coderight.jazz.event.ProgressEvent;
import nl.coderight.jazz.event.MessageEvent.MessageEventType;
import nl.coderight.jazz.event.ProgressEvent.ProgressEventType;

public class LoadPmHistTask extends Task<List<PmHist>> {
	@Override
	public List<PmHist> process() throws Exception{
		return DatabaseService.getInstance().getPmHists();
	}
	
	@Override
	protected void onStart() {
		ProgressEvent progressEvent = new ProgressEvent(ProgressEventType.START);
		progressEvent.setMessage("Loading pmhist...");
		progressEvent.setIndeterminate(true);
		postEvent(progressEvent);
	}
	
	@Override
	protected void onFinish() {
		postEvent(new ProgressEvent(ProgressEventType.STOP));
		CostStatisticEvent pmHistEvent = new CostStatisticEvent(CostStatisticEventType.LOAD_SUCCESS);
		pmHistEvent.setPmHists(getResult());
		postEvent(pmHistEvent);
	}
	
	@Override
	protected void onError() {
		MessageEvent error = new MessageEvent(MessageEventType.ERROR);
		error.setMessage(getError().getMessage());
		postEvent(error);			
	}
}
